﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Benefit.Models.Sys;
using Benefit.Models.Day;
namespace Benefit.Service.Day
{
    public class ServiceAccountDayAnalysis : Benefit.Interface.Day.IAccountDayAnalysis
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountDayAnalysis(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        /// <summary>
        /// 删除某日的所有数据
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.AccountDayAnalysis where t.TradeHistoryId == tradeHistoryId select t;
            foreach (AccountDayAnalysis d in query)
            {
                db.AccountDayAnalysis.Remove(d);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 保存账号日分析情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitAccountDayAnalysis(int tradeHistoryId, string pdate)
        {
            Benefit.Interface.Sys.IAccount iaccount = new Sys.ServiceAccount(db);
            Benefit.Interface.ServerData.IT_Account itaccount = new ServerData.ServiceT_Account(db);
            List<Benefit.Models.ServerData.T_Account> serverAccounts = itaccount.GetList();
            Interface.ServerData.IT_TradeDetail_History tth = new ServerData.ServiceT_TradeDetail_History(db);

            Interface.ServerData.IT_SettlementMoney money = new ServerData.ServiceT_SettlementMoney(db);

            Benefit.Interface.Day.IDayWinOrLoseTick itick = new ServiceDayWinOrLoseTick(db);
            Benefit.Interface.Config.IDefaultValue idvalue = new Config.ServiceDefaultValue(db);

            foreach (Benefit.Models.ServerData.T_Account account in serverAccounts)
            {
                Benefit.Models.Day.AccountDayAnalysis change = new AccountDayAnalysis();


                change.AccountId = iaccount.GetAccountId(account.LoginAccount);

                //   Models.ServerData.T_TradeDetail_History tth = new Models.ServerData.T_TradeDetail_History();

                //  Models.ServerData.T_SettlementMoney money = new ServerData.T_SettlementMoney();
                Models.ServerData.T_SettlementMoney _money = money.GetSettlementMoney(account.Id, pdate);
                int wincount = tth.GetDayWinCount(pdate, account.Id);
                int losecount = tth.GetDayLoseCount(pdate, account.Id);
                int pingcount = tth.GetDayPingCount(pdate, account.Id);
                //if ((wincount + losecount + pingcount) > 0)
                //{
                int allbillcount = tth.GetDayBillCount(pdate, account.Id);
                if (allbillcount == 0)
                {
                    change.AvgBillBenefit = 0;
                }
                else
                {
                    change.AvgBillBenefit = Math.Round((_money.CloseProfit - _money.Commission) / allbillcount, 2);
                }
                change.AvgPostionTime = tth.GetAccountAvgPostionTime(pdate, account.Id, change.AccountId, tradeHistoryId);
                change.MaxReturn = tth.GetMaxReturn(pdate, account.Id);


                //Data.Day.DayWinOrLoseTick tick = new DayWinOrLoseTick();
                change.OverStopLoss = itick.GetOverStopLoss(pdate, account.Id, idvalue.GetOverLostTick());
                change.PingJinPing = pingcount;
                change.RaiseRate = money.GetRaiseRate(account.Id, pdate);
                change.RiskCount = Convert.ToInt32(change.OverStopLoss);
                change.TradeHistoryId = tradeHistoryId;
                if (_money.PresetTlePosit > 0)
                {
                    change.Utilization = Math.Round(tth.GetAllOpenMoney(pdate, account.Id) / _money.PresetTlePosit, 2);
                }
                else
                {
                    change.Utilization = 0;
                }
                double winrate = 0;
                if ((wincount + losecount + pingcount) == 0)
                {
                    winrate = 1f;
                }
                else
                {
                    if (wincount == 0)
                    {
                        winrate = 0;
                    }
                    else
                    {
                        winrate = wincount / (wincount + losecount + pingcount);
                    }

                }

                change.WinRate = Math.Round(winrate, 2);
                if (_money.PresetTlePosit == 0)
                {
                    if ((_money.CloseProfit - _money.Commission) > 0)
                    {
                        change.Yield = Math.Round((_money.CloseProfit - _money.Commission), 2);
                    }
                    else
                    {
                        change.Yield = 0;
                    }

                }
                else
                {
                    if ((_money.CloseProfit - _money.Commission) > 0)
                    {
                        change.Yield = Math.Round((_money.CloseProfit - _money.Commission) / _money.PresetTlePosit, 2);
                    }
                    else
                    {
                        change.Yield = 0;
                    }

                }
                db.AccountDayAnalysis.Add(change);
                //}

            }
            db.SaveChanges();
        }


        public AccountDayAnalysis GetAccountDayAnalysis(int tradeHistoryId, int accountid)
        {
            AccountDayAnalysis returnValue = new AccountDayAnalysis();
            var query = db.AccountDayAnalysis.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                returnValue = query.First();
            }
            return returnValue;
        }
    }
}